package edu.byui.chat;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

public class Chat {
	private HashMap<String, ArrayList<Message>> map;
	protected String fileName;
	
	public Chat() {
		map = new HashMap<String, ArrayList<Message>>();
	}

	@SuppressWarnings("unchecked")
	public Chat(String fileName) {
		try {
			FileInputStream fileHandler = new FileInputStream(fileName);
			ObjectInputStream objectHandler = new ObjectInputStream(fileHandler);
			this.map = (HashMap<String, ArrayList<Message>>)objectHandler.readObject();
			System.out.println("Successfully loaded the session located in:");
			System.out.println(fileName);
			this.fileName = fileName;
		}
		catch (FileNotFoundException e) {
			System.out.println("No file found in the specified location.");
			this.map = new HashMap<String,ArrayList<Message>>();
			System.out.println("<Created NEW map>");
		}
		catch (IOException e) {
			System.out.println("There was an IO Exception loading a file in the CHAT Class!");
			e.printStackTrace();
			this.map = new HashMap<String,ArrayList<Message>>();
			System.out.println("<Created NEW map>");
		}
		catch (ClassNotFoundException e) {
			System.out.println("The file given was either CORRUPT or INAPPROPRIATE!");
			e.printStackTrace();
			this.map = new HashMap<String,ArrayList<Message>>();
			System.out.println("<Created NEW map>");
		}
	}

	public void addMessage(Message aMessage) {
		String to = aMessage.getTo();
		if (map.get(to) != null) {
			ArrayList<Message> list = (ArrayList<Message>)map.get(to);
			list.add(aMessage);
			map.put(to, list);
		}
		else {
			ArrayList<Message> list = new ArrayList<Message>();
			list.add(aMessage);
			map.put(to, list);
		}
	}

	public void save(String fileName) {
		try {
			FileOutputStream fileHandler = new FileOutputStream(fileName);
			ObjectOutputStream objectHandler = new ObjectOutputStream(fileHandler);
			objectHandler.writeObject(map);
		}
		catch (IOException e) {
			System.out.println("There was an error in the CHAT Class while writing the session to file:");
			System.out.println(this.fileName);
		}
	}

	public void save() throws Exception {
		if (this.fileName != null || this.fileName.isEmpty() == false) {
			try {
				FileOutputStream fileHandler = new FileOutputStream(this.fileName);
				ObjectOutputStream objectHandler = new ObjectOutputStream(fileHandler);
				objectHandler.writeObject(map);
			}
			catch(IOException e) {
				System.out.println("An IO Exception occurred while writing the CHAT SESSION to the file:");
				System.out.println(this.fileName);
				e.printStackTrace();
			}
		}
		else {
			throw(new Exception());
		}
	}

	public ArrayList<Message> getMessagesFor(String name) {
		return (ArrayList<Message>)map.get(name);
	}
}